package com.tiancheng.trade.authserver.config;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.oauth2.config.SaOAuth2ServerConfig;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName MySaOAuth2ServerConfig
 * @Description 登录服务配置
 * @Author kellen
 * @Date 2024/10/20 22:12
 */
@Slf4j
@Configuration
public class MySaOAuth2ServerConfig {

    @Autowired
    public void configOAuth2Server(SaOAuth2ServerConfig oauth2Server) {
        // 未登录的视图
        oauth2Server.notLoginView = () -> {
            log.error("登录先。。。");
            return new ModelAndView("login");
        };

        // 登录处理函数
        oauth2Server.doLoginHandle = (name, pwd) -> {
            SaHolder.getRequest();
            if ("sa".equals(name) && "123456".equals(pwd)) {
                StpUtil.login(10001);
                return SaResult.ok();
            }
            return SaResult.error("账号名或密码错误");
        };

        // 授权确认视图
        oauth2Server.confirmView = (clientId, scopes) -> {
            Map<String, Object> map = new HashMap<>();
            map.put("clientId", clientId);
            map.put("scope", scopes);
            return new ModelAndView("confirm", map);
        };
    }

}

